<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <!-- 搜索 -->
        <div slot="header" class="clearfix">
          <el-form ref="form" :model="form" label-width="80px" inline>
            <el-form-item label="目录名称" prop="directoryName">
              <el-input v-model="form.directoryName" @keyup.enter="handleFilter"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" placeholder="下拉选择" @keyup.enter="handleFilter">
                <el-option label="启用" value="0"></el-option>
                <el-option label="禁用" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="searchClear">搜索清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
            <el-form-item>
              <el-button style="float: right; !padding: 3px 0" type="success" icon="el-icon-edit" @click="addFn"
                >新增目录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="msg">
          <el-alert type="info"><i class="el-icon-info"></i> 共有{{ totals }}条记录</el-alert>
        </div>
        <!-- 页面 -->
        <el-table :data="lists">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="subjectName" label="所属学科"></el-table-column>
          <el-table-column prop="directoryName" label="目录名称"></el-table-column>
          <el-table-column prop="username" label="创建者"></el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template #default="{ row }">
              {{ $dataFormat(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"></el-table-column>
          <el-table-column prop="state" label="状态">
            <template v-slot="scope">
              <!-- <span>{{ scope.row.state === 0 ? '已启用' : '已禁用' }}</span> -->
              <span v-if="!scope.row.state">已启用</span>
              <span v-else>已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button type="text" @click="changestateFn(scope.row)" v-if="scope.row.state == 1">启用</el-button>
              <el-button type="text" @click="changestateFn(scope.row)" v-else>禁用</el-button>
              <el-button type="text" @click="editDataFn(scope.row)" :disabled="scope.row.state == 0 ? false : true"
                >修改</el-button
              >
              <el-button type="text" @click="deleteFn(scope.row)" :disabled="scope.row.state == 0 ? false : true"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <template>
          <div class="block">
            <span class="demonstration"></span>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage4"
              :page-sizes="[5, 10, 20, 30, 40]"
              :page-size="requestParameters.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totals"
              style="float: right"
            >
            </el-pagination>
          </div>
        </template>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog
        :title="isEdit == false ? '添加目录' : '修改目录'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="roleRules" label-width="100px">
          <el-form-item label="所属学科" prop="subjectID">
            <!-- <el-input v-model="roleForm.subjectID"/> -->

            <el-select v-model="roleForm.subjectID" placeholder="请选择">
              <el-option
                v-for="(item, index) in lists"
                :key="index"
                :label="item.subjectName"
                :value="item.subjectID"
              ></el-option>
              <!-- <el-option label="大数据" value="16"> </el-option> -->
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="roleForm.directoryName" />
          </el-form-item>
        </el-form>

        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="cancleRoles">取消</el-button>
            <el-button size="small" type="primary" @click="roleSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState, add, update } from '@/api/hmmm/directorys.js'
export default {
  data () {
    return {
      showDialog: false, // 控制弹框的隐藏和展示
      isEdit: false,
      editId: '',
      // 添加角色
      roleForm: {
        subjectID: '',
        directoryName: ''
      },
      // 添加角色验证
      roleRules: {
        subjectID: [{ required: true, message: '学科id不能为空', trigger: 'blur' }],
        directoryName: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      },
      visible: false,
      props: ['addShow'],
      addData: {},
      addShow: false,
      totals: 0, // z总条数
      lists: [], //  原本的数组
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: this.page,
      form: {
        directoryName: '',
        state: '',
        page: 1,
        pagesize: 10
      },
      requestParameters: {
        page: 1,
        pagesize: 10,
        directoryName: ''
      },
      // directoryName: '',
      query: {
        page: '1', // 当前选中的页码
        pagesize: 10, // 选中页容量
        subjectID: '',
        directoryName: '',
        state: ''
      }
    }
  },
  created () {
    this.directorysAPI()
  },
  methods: {
    // 添加权限按钮(弹窗的)
    roleSubmit () {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            const res = await update({
              id: this.editId,
              ...this.roleForm
            })
            // console.log(res)
          } else {
            // console.log(333)
            // 调用新增角色的 API
            const res = await add(this.roleForm)
            // console.log(res)
          }
          // 根据状态码判断请求成功与否
          // if (!res.status === 200) return this.$message.error('添加失败')
          // 添加成功，给用户进行提示
          this.$message.success('操作成功')
          // 重新获取权限列表数据
          this.directorysAPI()
          // 隐藏弹框
          this.showDialog = false
        }
      })
    },

    // 取消权限按钮(弹窗的)
    cancleRoles () {
      this.showDialog = false
    },
    // 搜索删除
    searchClear () {
      this.form.directoryName = ''
      this.form.state = ''
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.directorysAPI(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.directorysAPI()
    },
    // 渲染列表
    async directorysAPI () {
      if (this.$route.query.id) {
        // console.log(this.$route.query.id)
        const res = await list({ subjectID: this.$route.query.id })
        // console.log(res)
        this.lists = res.data.items
        this.totals = res.data.counts
      } else {
        const res = await list(this.requestParameters)
        // console.log(res)
        this.lists = res.data.items
        this.totals = res.data.counts
      }
    },
    // 改变按钮状态(启用禁用)
    async changestateFn (row) {
      await changeState({
        id: row.id,
        state: row.state === 0 ? 1 : 0
      })
      if (!this.state) {
        row.state = !row.state
      }
      this.$message.success('操作成功')
    },
    // 新增
    addFn () {
      this.showDialog = true
      this.isEdit = false
    },
    // 修改
    editDataFn (dataObj) {
      this.isEdit = true
      // console.log(dataObj)
      this.roleForm.subjectID = dataObj.subjectID
      this.roleForm.directoryName = dataObj.directoryName
      this.editId = dataObj.id
      this.showDialog = true
      // await update(id)?
      // this.directorysAPI()
    },
    // 删除
    async deleteFn (data) {
      // console.log('1')
      await remove(data)

      this.directorysAPI()
    },
    // 点击搜索, 进行筛选(#######################有问题)
    async search () {
      // const res = await list(this.form)
      // console.log(res)
      const { data: res } = await list({ directoryName: this.form.directoryName, state: this.form.state })
      this.lists = res.items
      this.total = res.counts
    }
  }
}
</script>

<style scoped lang="less"></style>
<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

/* .box-card {
    width: 480px;
  } */
</style>
